<?php
	/* Functions in this file */
	/**************************/

	// DeleteFretNumberArrayContents()
	// GetChordName()
	// GetChordNamePtr()
	// GetFretNumber($string)
	// GetMFCClassName()
	// GetMFCClassSchema()
	// GetSpelling()
	// GetStringCount()
	// GetTopFret()
	// IsSameVoicing(&$chordDiagram)
	// IsValidFretNumber($fretNumber)
	// IsValidString($string)
	// IsValidTopFret($topFret)
	// SetChordName(&$chordName)
	// SetTopFret($topFret)
?>
 
<?php
	 
	class ChordDiagram {
		 
		 
		// Default Constants
		const DEFAULT_TOP_FRET = 0;
		 
		// Top $this->Fret Constants
		const MIN_TOP_FRET = 0;
		const MAX_TOP_FRET = 20;
		 
		// $this->Fret Number Constants
		const MIN_FRET_NUMBER = 0;
		const MAX_FRET_NUMBER = 24;
		 
		 
		const stringMuted = 0x000000fe;
		const notUsed = 0x000000ff;
		 
		 
		// Member Variables
		 
		protected $m_chordName;
		protected $m_topFret = self::DEFAULT_TOP_FRET;
		protected $m_fretNumberArray = array ();
		 
		 
		// MFC Class Functions
		 
		 
		function GetMFCClassName() {
			return "CChordDiagram";
		}
		 
		 
		function GetMFCClassSchema() {
			return 1;
		}
		 
		 
		// Chord Name Functions
		 
		 
		function SetChordName(&$chordName) {
			$this->m_chordName = $chordName;
		}
		 
		 
		function GetChordName() {
			return ($this->m_chordName);
		}
		 
		 
		function & GetChordNameRef() {
			return ($this->m_chordName);
		}
		 
		 
		static function & GetChordNameConstRef() {
			return ($this->m_chordName);
		}
		 
		 
		//function GetChordNamePtr(){
		//return (&$this->m_chordName);
		//}
		 
		// Top $this->Fret Functions
		 
		 
		 
		function IsValidTopFret($topFret) {
			return ($topFret <= self::MAX_TOP_FRET);
		}
		 
		 
		 
		function SetTopFret($topFret) {
			if (!$this->IsValidTopFret($topFret)) return 0;
			$this->m_topFret = $topFret;
			return 1;
		}
		 
		 
		function GetTopFret() {
			return ($this->m_topFret);
		}
		 
		// String Functions
		 
		 
		 
		function IsValidString($string) {
			return ($string < $this->GetStringCount());
		}
		 
		 
		function GetStringCount() {
			return sizeof($this->m_fretNumberArray);
		}
		 
		// $this->Fret Number Functions
		 
		 
		 
		function IsValidFretNumber($fretNumber) {
			return (($fretNumber <= self::MAX_FRET_NUMBER) || ($fretNumber == self::stringMuted) || ($fretNumber == self::notUsed));
		}
		 
		function DeleteFretNumberArrayContents() {
			$this->m_fretNumberArray = array ();
		}
		 
		 
		 
		 
		function ChordDiagram(&$chordName = NULL, $topFret = NULL, $fretNumber1 = NULL, $fretNumber2 = NULL, $fretNumber3 = NULL, $fretNumber4 = NULL, $fretNumber5 = NULL, $fretNumber6 = NULL, $fretNumber7 = NULL) {
			 
			$this->m_chordName = new ChordName ();
			if (!$this->IsValidTopFret($topFret)) return 0;
			$this->SetChordName($chordName);
			$this->AddFretNumbers($fretNumber1, $fretNumber2, $fretNumber3, $fretNumber4, $fretNumber5, $fretNumber6, $fretNumber7);
		}
		 
		 
		
		// Serialize Functions
		 
		 
		 
		function DoSerialize(&$stream){
		 
		
			$this->m_chordName->DoSerialize($stream);

			WRITE_BYTE ($stream,$this->m_topFret);

			$count = $this->GetStringCount();
			WRITE_BYTE ($stream,$count);

			$i = 0;
			for (; $i < $count; $i++) {
				WRITE_BYTE ($stream,$this->m_fretNumberArray[$i]);
			}

			return 1;
		}
		
		 
		 
		 
		 
		function DoDeserialize(&$stream){
		 
			$this->m_chordName = new ChordName ();
			
			$this->m_chordName->DoDeserialize($stream);
		 
			$this->m_topFret = READ_BYTE ($stream);
		 
			$count = READ_BYTE ($stream);
		
			$i = 0;
			for (; $i < $count; $i++) {
		 
				$fretNumber = READ_BYTE ($stream);
		 
				array_push($this->m_fretNumberArray,$fretNumber);
			}
		 
			return 1;
		}
		
		// Chord Diagram Functions
		 
		 
		 
		function SetChordDiagram(&$chordName, $topFret, $fretNumber1, $fretNumber2, $fretNumber3, $fretNumber4, $fretNumber5, $fretNumber6, $fretNumber7) {
			 
			//------Last Checked------//
			// - Jan 15, 2005
			$this->SetChordName($chordName);
			if (!$this->SetTopFret($topFret))
				return 0;
			return ($this->AddFretNumbers($fretNumber1, $fretNumber2, $fretNumber3, $fretNumber4, $fretNumber5, $fretNumber6, $fretNumber7));
		}
		 
		// $this->Fret Number Functions
		 
		 
		 
		 
		function SetFretNumber($string, $fretNumber) {
			 
			//------Last Checked------//
			// - Jan 15, 2005
			if (!$this->IsValidString($string)) return 0;
			if (!$this->IsValidFretNumber($fretNumber)) return 0;
			$this->m_fretNumberArray[$string] = $fretNumber;
			return 1;
		}
		 
		 
		 
		 
		function GetFretNumber($string) {
			 
			//------Last Checked------//
			// - Jan 15, 2005
			if (!$this->IsValidString($string)) return 0;
			return ($this->m_fretNumberArray[$string]);
		}
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		function AddFretNumbers($fretNumber1, $fretNumber2, $fretNumber3, $fretNumber4, $fretNumber5, $fretNumber6, $fretNumber7) {
			 
			//------Last Checked------//
			// - Jan 15, 2005
			if (!$this->IsValidFretNumber($fretNumber1)) return 0;
			if (!$this->IsValidFretNumber($fretNumber2)) return 0;
			if (!$this->IsValidFretNumber($fretNumber3)) return 0;
			if (!$this->IsValidFretNumber($fretNumber4)) return 0;
			if (!$this->IsValidFretNumber($fretNumber5)) return 0;
			if (!$this->IsValidFretNumber($fretNumber6)) return 0;
			if (!$this->IsValidFretNumber($fretNumber7)) return 0;
			 
			$this->DeletefretNumberArrayContents();
			 
			array_push($this->m_fretNumberArray, $fretNumber1);
			array_push($this->m_fretNumberArray, $fretNumber2);
			array_push($this->m_fretNumberArray, $fretNumber3);
			 
			// CASE: Notes 4-7 are not mandatory, if set to self::notUsed, ignore them
			if ($fretNumber4 == self::notUsed)
				return 1;
			array_push($this->m_fretNumberArray, $fretNumber4);
			 
			if ($fretNumber5 == self::notUsed)
				return 1;
			array_push($this->m_fretNumberArray, $fretNumber5);
			 
			if ($fretNumber6 == self::notUsed)
				return 1;
			array_push($this->m_fretNumberArray, $fretNumber6);
			if ($fretNumber7 == self::notUsed)
				return 1;
			array_push($this->m_fretNumberArray, $fretNumber7);
			 
			return 1;
		}
		 
		// Voicing Functions
		 
		 
		 
		function IsSameVoicing(&$chordDiagram) {
			$thisStringCount = $this->GetStringCount();
			$otherStringCount = $chordDiagram->GetStringCount();
			 
			// Chord diagrams have a different number of strings
			if ($thisStringCount != $otherStringCount)
				return 0;
			 
			// Check each string for matching fret numbers
			$i = 0;
			for (; $i < $thisStringCount; $i++) {
				if ($this->m_fretNumberArray[$i] != $chordDiagram->m_fretNumberArray[$i])
					return 0;
			}
			 
			return 1;
		}
		 
		 
		 
		function IsSameVoicingFrets ($fretNumber1, $fretNumber2, $fretNumber3, $fretNumber4 = self::notUsed, $fretNumber5 = self::notUsed, $fretNumber6 = self::notUsed, $fretNumber7 = self::notUsed) {
			$temp = new ChordDiagram();
			$temp->AddFretNumbers($fretNumber1, $fretNumber2, $fretNumber3, $fretNumber4, $fretNumber5, $fretNumber6, $fretNumber7);
			return ($this->IsSameVoicing($temp));
		}
		 
		// Operations
		 
		function GetSpelling() {
			$returnValue = "";
			 
			$i = $this->GetStringCount();
			for (; $i > 0; $i--) {
				$fretNumber = "";
				if ($this->m_fretNumberArray[$i - 1] == self::stringMuted)
					$returnValue .= "x";
				else
					$fretNumber = sprintf("%d", $this->m_fretNumberArray[$i - 1]);
				$returnValue .= $fretNumber;
				 
				// Add a space between numbers
				if ($i > 1)
					$returnValue .= " ";
			}
			 
			return $returnValue;
		}
	}
	 
?>


